package com.ithaipeng.redanttakeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithaipeng.redanttakeaway.dto.SetmealDto;
import com.ithaipeng.redanttakeaway.exception.CustomException;
import com.ithaipeng.redanttakeaway.mapper.SetmealMapper;
import com.ithaipeng.redanttakeaway.pojo.Setmeal;
import com.ithaipeng.redanttakeaway.pojo.SetmealDish;
import com.ithaipeng.redanttakeaway.service.ISetmealDishService;
import com.ithaipeng.redanttakeaway.service.ISetmealService;
import com.ithaipeng.redanttakeaway.tools.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Autowired
    private ISetmealDishService setmealDishService;
    @Override
    public void insertWithDish(SetmealDto setmealDto) {
        //1 保存到setmeal表
        this.save(setmealDto);
        //2 插入到setmeal_表里
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishList = setmealDishes.stream().peek((item) -> {
            item.setSetmealId(setmealDto.getId());
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    public void deleteWishDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(setmealLambdaQueryWrapper);
        if(count>0){
            throw new CustomException("删除的套餐里有起售的套餐，不能删除!");
        }else {
            //先删除setmeal表
            this.removeByIds(ids);
            //再删除setmeal_dish表
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
            setmealDishService.remove(setmealDishLambdaQueryWrapper);
        }
    }

    @Override
    public void statusUpDate(List<Long> ids, Integer status) {
        LambdaUpdateWrapper<Setmeal> setmealLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        setmealLambdaUpdateWrapper.in(Setmeal::getId,ids);
        setmealLambdaUpdateWrapper.set(Setmeal::getStatus,status);
        setmealLambdaUpdateWrapper.set(Setmeal::getUpdateTime,LocalDateTime.now());
        setmealLambdaUpdateWrapper.set(Setmeal::getUpdateUser, BaseContext.getCurrentId());
        this.update(setmealLambdaUpdateWrapper);
    }

    @Override
    public void updateByDish(SetmealDto setmealDto) {
        //先修改
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        //删除后再保存
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishList = setmealDishes.stream().peek((item) ->
                item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishList);
    }
}
